package action.country;

import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import model.dao.CountryModel;
import model.dto.Country;
import controller.Action;
import controller.ActionForward;

public class ActionGetAllCountries implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Country dto=null;
		CountryModel dao=new CountryModel();
		
		ArrayList<Country> list=new ArrayList<Country>();
		ResultSet rs=dao.list();
		while(rs.next()){
			dto=new Country();
			dto.setId(rs.getInt("country_id"));
			dto.setName(rs.getString("country_name"));
			list.add(dto);
		}
		String json=new Gson().toJson(list);
		System.out.println(json);
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(json);
		return null;
	}

}
